package com.zoro.barn.business.service.system.business.menu.controller;

import com.zoro.barn.business.service.system.business.teamuser.entity.BarnTeamUser;
import com.zoro.barn.business.service.system.business.teamuser.service.BarnTeamUserService;
import com.zoro.barn.business.service.system.business.menu.entiity.BarnMenu;
import com.zoro.barn.business.service.system.business.menu.service.BarnMenuService;
import com.zoro.barn.commons.webcommons.tools.WebContentUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("menu-user")
public class BarnMenuUserController {

    private final BarnMenuService barnMenuService;
    private final BarnTeamUserService barnTeamUserService;

    public BarnMenuUserController(BarnMenuService barnMenuService, BarnTeamUserService barnTeamUserService) {
        this.barnMenuService = barnMenuService;
        this.barnTeamUserService = barnTeamUserService;
    }

    /**
     * 获取登陆人 menus
     *
     * @return menus
     */
    @RequestMapping("/getUserMenus")
    public List<BarnMenu> getUserMenus() {

        String userId = WebContentUtil.requestUserInfo().getPid();

        BarnTeamUser teamUser = new BarnTeamUser();
        teamUser.setUserId(userId);
        List<BarnTeamUser> teams = barnTeamUserService.query(teamUser);
        Set<String> teamIds = null;
        if (CollectionUtils.isNotEmpty(teams)) {
            teamIds = teams.stream().map(BarnTeamUser::getTeamId).collect(Collectors.toSet());
        }

        return barnMenuService.getByUserId(userId, teamIds);
    }

}
